<?php
namespace App\Services;
use App\Models\MemberAddressModel;

class CenterService{
    /**
     * 获取收货地址详细信息
     * @param int $address_id
     * @return object $address_info
     */
    public function getAddressInfoById($address_id){
        $addressInfo = MemberAddressModel::where('id',$address_id)->first();
        return $addressInfo;
    }

    /**
     * 获取默认收货地址
     * @param int $member_id
     * @return object $info
     */
    public function getDefaultAddress($member_id){
        $info = MemberAddressModel::where('member_id',$member_id)->where('is_default','yes')->first();
        return $info;
    }

    /**
     * 获取收货地址列表
     * @param int $member_id
     * @return object $address_list
     */
    public function getAddressList($member_id){
        $address_list = MemberAddressModel::orderBy('is_default','desc')->where('member_id',$member_id)->get();
        return $address_list;
    }

    /**
     * 添加收货地址
     * @param array $data
     * @return bool $res
     */
    public function insertAddress($data){
        $res = MemberAddressModel::insert($data);
        return $res;
    }

    /**
     * 更新收货地址
     * @param int $id
     * @param array $data
     * @return bool $res
     */
    public function updateAddress($id,$data){
        $res = MemberAddressModel::where('id',$id)->update($data);
        return $res;
    }

    /**
     * 清除默认地址标识
     * @param int $member_id
     * @return bool $res
     */
    public function clearDefault($member_id){
        $res = MemberAddressModel::where('member_id',$member_id)->update(['is_default'=>'no']);
        return $res;
    }

    /**
     * 删除收货地址
     * @param int $id
     * @return bool $res
     */
    public function delAddress($id){
        $res = MemberAddressModel::where('id',$id)->delete();
        return $res;
    }
}